﻿Public Class Language
  Private Shared _langDefault As Dictionary(Of String, String) = Nothing
  Private Shared _lang As New Dictionary(Of String, String)

  Public Shared Sub LoadLanguage(ByVal langFolderPath As String, ByVal defaultFolderPath As String)
    LoadLanguageFile(_lang, langFolderPath)
    If defaultFolderPath IsNot Nothing AndAlso defaultFolderPath.Length > 0 Then
      _langDefault = New Dictionary(Of String, String)
      LoadLanguageFile(_langDefault, defaultFolderPath)
    End If
  End Sub

  Private Shared Sub LoadLanguageFile(ByVal dic As Dictionary(Of String, String), ByVal iniFolderPath As String)
    Dim ini As New Ini.File
    Dim inisec As Ini.Section

    Try
      ' Clear out the language dictionary
      dic.Clear()

      ' Look for all ini files in this path
      For Each f In IO.Directory.GetFiles(iniFolderPath, "*.ini")
        Try
          ' Clear the ini
          ini.Clear()

          ' Load the langauge file
          ini.LoadFile(f)

          ' Fetch the 'strings' section, which is what contains all the language strings
          inisec = ini.SectionNoNull("Strings")

          ' Fetch all the strings contained in this language file and add it to the language collection
          For Each key As String In inisec.Keys
            dic.Add(key.ToLower, inisec.StringValueNoNull(key))
          Next
        Catch ex As Exception
          System.Windows.Forms.MessageBox.Show("Unable to load language file " & f & ".  Error: " & ex.Message)
        End Try
      Next
    Catch ex As Exception
      System.Windows.Forms.MessageBox.Show("Unable to load language folder " & iniFolderPath & ".  Error: " & ex.Message)
    End Try
  End Sub

  Public Shared ReadOnly Property GetLanguageString(ByVal key As String) As String
    Get
      Dim lkey As String = key.ToLower
      If _lang.ContainsKey(lkey) Then
        Return _lang(lkey)
      ElseIf _langDefault IsNot Nothing AndAlso _langDefault.ContainsKey(lkey) Then
        Return "[ENG] " & _langDefault(lkey)
      Else
        Return "<Missing: " & key & ">"
      End If
    End Get
  End Property

  ''' <summary>
  ''' Same as GetLanguageString, except instead of returning a 'missing' tag, it returns an empty string (good for optional language components)
  ''' </summary>
  Public Shared ReadOnly Property GetLanguageStringNoMissing(ByVal key As String) As String
    Get
      Dim lkey As String = key.ToLower
      If _lang.ContainsKey(lkey) Then
        Return _lang(lkey)
      ElseIf _langDefault IsNot Nothing AndAlso _langDefault.ContainsKey(lkey) Then
        Return "[ENG] " & _langDefault(lkey)
      Else
        Return String.Empty
      End If
    End Get
  End Property

  Public Shared ReadOnly Property IsLanguageKeyValid(ByVal key As String) As Boolean
    Get
      Return _lang.ContainsKey(key.ToLower)
    End Get
  End Property

  Public Shared Function GetLanguageReader() As LanguageReader
    Return New LanguageReader(_lang, _langDefault)
  End Function

  Public Class LanguageReader
    Private _lang As New Dictionary(Of String, String)
    Private _langDefault As Dictionary(Of String, String) = Nothing

    Public Sub New(ByVal lang As Dictionary(Of String, String), ByVal langDefault As Dictionary(Of String, String))
      Me._lang = lang
      Me._langDefault = langDefault
    End Sub

    Public ReadOnly Property GetString(ByVal key As String) As String
      Get
        Dim lkey As String = key.ToLower
        If Me._lang.ContainsKey(lkey) Then
          Return Me._lang(lkey)
        ElseIf _langDefault IsNot Nothing AndAlso _langDefault.ContainsKey(lkey) Then
          Return "[ENG] " & _langDefault(lkey)
        Else
          Return Nothing
        End If
      End Get
    End Property
  End Class
End Class
